package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBorrowerFlowInformationService;
import cn.stylefeng.guns.sys.modular.system.service.BtyDkmxService;
import cn.stylefeng.guns.sys.modular.system.service.BtyXdgtService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 借款人流水信息控制器
 *
 * @author liuy
 * @Date
 */
@Controller
@RequestMapping("/btyDkmx")
public class BtyDkmxController extends BaseController {

    private String PREFIX = "/modular/system/btyDkmx/";

    @Resource
    private BtyBorrowerFlowInformationService borrowerFlowInformationService;
    private String userName = "admin";
    @Resource
    private BtyXdgtService btyXdgtService;
    @Resource
    private BtyDkmxService btyDkmxService;

    /**
     * 跳转到打款明细列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyDkmx.html";
    }

    /**
     * 跳转到借贷款列表首页
     */
    @RequestMapping("/upload")
    public String upload() {
        return PREFIX + "uploadTxt.html";
    }

    /**
     * 跳转到打印模板頁
     */
    @RequestMapping("/printMould/{id}")
    public String printMould(@PathVariable Long id, ModelMap map) {
        map.put("id", id);
        return PREFIX + "printMould.html";
    }

    /**
     * 导入txt
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUser(@RequestParam("file") MultipartFile file) {
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        String fileName = file.getOriginalFilename();
        try {
            flag = btyDkmxService.batchImportTxt(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;

    }

    /**
     * 获取打款明细列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition, String flag) {
        //获取用户的帐号
        Long userId = LoginContextHolder.getContext().getUserId();
        String username = LoginContextHolder.getContext().getUser().getName();
        Page<Map<String, Object>> list = this.btyDkmxService.list(condition, flag);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 获取打印数据
     */
    @RequestMapping(value = "/msg")
    @ResponseBody
    public Object msg(String id, String condition, String flag) {
        return btyDkmxService.msg(id, condition, flag);
    }

    /**
     * 修改打印状态
     */
    @RequestMapping(value = "/updateFlag")
    @ResponseBody
    public int updateFlag(@RequestParam String id) {
        return this.btyDkmxService.updateFlag(id);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        this.btyDkmxService.removeById(id);
        return SUCCESS_TIP;
    }
}
